﻿#region Header
/********************************************************           
 * RequestHandler         
 *                                                         
 * Author:  Richard Barclay  (phphanatic at gmail dot com) 
 * 
 * Copyright: 2009 Richard Barclay
 * 
 * License: GNU General Public License (GPL)
 *     
 * This file is part of the Intergr8.GoogleVisualization library.
 *
 *   The Intergr8.GoogleVisualization library is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *  Intergr8.GoogleVisualization library is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with Intergr8.GoogleVisualization library.  If not, see <http://www.gnu.org/licenses/>.
 *                                                         
 * Purpose:  Handle and parse the content of a web Request in the form
 *           of a NameValueCollection
 *                                                         
 * Usage:                                                  
 *      Initial entry point for handling and proceessing a 
 *     visualization data source request
 ********************************************************/
#endregion



namespace Intergr8.GoogleVisualization
{
    using System;
    using System.Collections.Specialized;
    using System.Text;

    /// <summary>
    /// Parse a DataSource request
    /// </summary>
    public class RequestHandler : VisualizationBase, IRequestHandler
    {

        #region Constants
        private static string ERROR_PARAMETERMISSING = "A request object (NameValueCollection) must be supplied";
        private static string ERROR_CONSTRUCTOREXCEPTION = "RequestHandler::~ctor. Exception {0} ";

        private static string PARAM_TQ = "tq";
        private static string PARAM_TQX = "tqx";
        #endregion
		
		#region Attributes
        /// <summary>
        /// Goggle query api request string
        /// </summary>
        private TQ _tq = new TQ();

        /// <summary>
        /// Extended request options, handled by a TQX object
        /// </summary>
        private TQX _tqx = new TQX();

		#endregion

        #region Constructors

        /// <summary>
        /// Default constructor
        /// </summary>
        private RequestHandler()
        {
        }


        /// <summary>
        /// Constructor taking a NameValueCollection
        /// </summary>
        /// <param name="request"></param>
        private RequestHandler(NameValueCollection request)
        {
            try
            {
                if (request == null)
                {
                    throw new ArgumentNullException(ERROR_PARAMETERMISSING);
                }

                // Extract any Query
                if (!string.IsNullOrEmpty(request[PARAM_TQ]))
                {
                    _tq = new TQ(request[PARAM_TQ]);
                }

                // Extract the extended request details
                if (!string.IsNullOrEmpty(request[PARAM_TQX]))
                {
                    _tqx = new TQX(request[PARAM_TQX]);
                }

                //Mark the Handler as valid
                this._isValid = true;

            }
            catch (Exception ex)
            {
                System.Diagnostics.Trace.TraceError(ERROR_CONSTRUCTOREXCEPTION, ex.Message);
                this._isValid = false;
                throw ex;
            }

        }

        #endregion

        #region Class Factories
        /// <summary>
        /// Factory create method to manage construction of the object
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        public static RequestHandler CreateRequestHandler(NameValueCollection request)
        {
            RequestHandler requestHandler = new RequestHandler(request);
            return requestHandler;
        }
        #endregion

        #region Business Properties
        /// <summary>
        /// Flag identifying if the RequestHandler object is valid for processing.
        /// 
        /// Validity if this and all child objects
        /// </summary>
        public override bool IsValid
        {
            get { return this._isValid && _tqx.IsValid && _tq.IsValid; }
        }

        /// <summary>
        /// Return any Query that has been specified in the Request
        /// </summary>
        public string Query
        {
            get { return _tq.ToString(); }
        }

        /// <summary>
        /// Exposing the TQX object holdin additional request detail
        /// </summary>
        public TQX TQX
        {
            get
            {
                return _tqx;
            }
        }

        #endregion
    }
}
